#coding=utf-8
from __future__ import unicode_literals
#导入游戏模块
import pygame
#导入按键检测模块，用于检测按键事件
from pygame.locals import *
#导入事件模块
import time
#导入随机数模块
import random
#玩家飞机类
class HeroPlane(object):

    def __init__(self,screen):
        #设置飞机默认位置
        self.x = 200
        self.y = 570

        self.screen = screen

        self.imageName = "./image/hero1.png"
        #创建一个飞机
        self.image = pygame.image.load(self.imageName).convert_alpha()
        #获取飞机长宽
        self.imageX,self.imageY = self.image.get_size()

        #用来存储飞机发射出去的子弹
        self.bulletList = []
    #显示飞机
    def display(self):
        self.screen.blit(self.image,(self.x,self.y))
        #需要删除的子弹
        __needRemoveList = []

        for tempBullet in self.bulletList:
            tempBullet.display()
            tempBullet.move()
            if tempBullet.judgeOut():
                __needRemoveList.append(tempBullet)
        #删除self.bulletList中需要删除的对象
        for tempRemoveBullet in __needRemoveList:
            self.bulletList.remove(tempRemoveBullet)

        
            

    #移动飞机
    def moveLeft(self):
        self.x -= 10
        if self.x <= 0:
            self.x = 0
    def moveRight(self):
        self.x += 10
        if self.x >= g_gameWidth-100:
            self.x = g_gameWidth-100
    def moveUp(self):
        self.y -= 10
        if self.y <= 0:
            self.y = 0
    def moveDown(self):
        self.y += 10
        if self.y >= g_gameHeight-120:
            self.y = g_gameHeight-120
    #发射子弹
    def shootBullet(self):
        newBullet = Bullet(self.x,self.y,self.screen)
        self.bulletList.append(newBullet)
        
        
#玩家发射子弹类
class Bullet(object):
    def __init__(self,x,y,screen):

        self.x = x+41
        self.y = y-20
        #子弹的图片路径
        self.imageName = "./image/bullet.png"
        self.image = pygame.image.load(self.imageName)

        self.screen = screen
         
    #显示子弹
    def display(self):
        
        self.screen.blit(self.image,(self.x,self.y))
    #子弹移动
    def move(self):
        self.y -= 5 
    #判断子弹是否越界
    def judgeOut(self):
        if self.y < -25:
            return True
        else:
            return False

#电脑敌人飞机类
class EnemyPlane(object):

    def __init__(self,screen):
        #设置飞机默认位置
        self.x = 0
        self.y = 0

        self.screen = screen

        self.imageName = "./image/enemy0.png"
        #创建一个飞机
        self.image = pygame.image.load(self.imageName).convert_alpha()
        #获取飞机长宽
        self.imageX,self.imageY = self.image.get_size()

        #用来存储飞机发射出去的子弹
        self.bulletList = []

        #敌机方向
        self.direction = "right"
    #显示飞机
    def display(self):
        self.screen.blit(self.image,(self.x,self.y))
        #需要删除的子弹
        __needRemoveList = []
        for tempBullet in self.bulletList:
            tempBullet.display()
            tempBullet.move()

            if tempBullet.judgeOut():
                __needRemoveList.append(tempBullet)
        #删除self.bulletList中需要删除的对象
        for tempRemoveBullet in __needRemoveList:
            self.bulletList.remove(tempRemoveBullet)
            

    #移动飞机
    def move(self):
        if self.direction == "right":
            self.x += 2
        elif self.direction == "left":
            self.x -= 2

        if self.x>g_gameWidth-50:
            self.direction = "left"
        elif self.x<0:
            self.direction = "right"
    
    #发射子弹
    def shootBullet(self):
        #随机发射子弹
        num = random.randint(1,100)
        if num == 66:
            newBullet = EnemyBullet(self.x,self.y,self.screen)
            self.bulletList.append(newBullet)
        
#电脑发射子弹类
class EnemyBullet(object):
    def __init__(self,x,y,screen):

        self.x = x+23
        self.y = y+34
        #子弹的图片路径
        self.imageName = "./image/bullet1.png"
        self.image = pygame.image.load(self.imageName)

        self.screen = screen
         
    #显示子弹
    def display(self):
        
        self.screen.blit(self.image,(self.x,self.y))
        
    #子弹移动
    def move(self):
        self.y += 2 
    #判断子弹是否越界
    def judgeOut(self):
        if self.y > g_gameHeight-20:
            return True
        else:
            return False
#全局变量定义游戏界面大小
g_gameWidth = 480
g_gameHeight = 700

if __name__ == "__main__":
    
    #创建窗口,用来显示内容
    screen = pygame.display.set_mode((g_gameWidth,g_gameHeight),0,32)
    #创建一个和窗口大小图片，用来充当背景
    background = pygame.image.load("./image/background.png")


    heroplane = HeroPlane(screen)
    enemyplane = EnemyPlane(screen)

  
    while True:
        #显示背景
        screen.blit(background,(0,0))
        
        heroplane.display()
        enemyplane.display()
        enemyplane.move()
        enemyplane.shootBullet()
        

        #判断是否按下退出按钮
        for event in pygame.event.get():
            
            #退出
            if event.type == QUIT:
                print("exit")
                exit()
            #上下左右
            elif event.type == KEYDOWN:
                if event.key == K_ESCAPE: 
                    print("exit")                   
                    exit()
                    
                if event.key == K_a or event.key == K_LEFT:
                    heroplane.moveLeft()

                    print("左")
                    
                elif event.key == K_d or event.key == K_RIGHT:
                    heroplane.moveRight()

                    print("右")
                    
                elif event.key == K_w or event.key == K_UP:
                    heroplane.moveUp()
                    print("上")
                elif event.key == K_s or event.key == K_DOWN:
                    heroplane.moveDown()
                    print("下")
                elif event.key == K_SPACE:
                    heroplane.shootBullet()
                    print("空格")
                
                

        #更新界面
        pygame.display.update()
        